import {fetchPost} from '../service/api';

export default {
  namespace: 'server',

  state: {
    serverList: [],
    serverStatus: [],
  },

  effects: {
    *fetchServer({payload}, { put, call, select, all }){
      const response = yield call(fetchPost, "queryServer.do", payload);
      if(response){
        yield put({
          type: 'saveServer',
          payload: response.list,
        });
        const currentSys = yield select(state => state.sys.sysList.find(item => item.id == payload.sysId));
        let callArray = [];
        response.list.forEach(item => {
          if(item.grep){
            let serverInfo = getServer(item, currentSys);
            let pInfo = {
              ...serverInfo,
              grep: item.grep,
              id: item.id,
              signal: 'testStatus'
            };
            callArray.push(call(fetchPost, "execCommand.do", pInfo));
          }
        });
        const statusList = yield all(callArray);
        yield put({
          type: 'saveStatus',
          payload: statusList,
        });
      }
    },
    *addServer({payload}, { put, call }){
      const response = yield call(fetchPost, "addServer.do", payload);
      if(response){
        yield put({
          type: 'addServerItem',
          payload: Object.assign(payload, {id: response.id}),
        });
      }
      return response;
    },
    *updateServer({payload}, { put, call }){
      const response = yield call(fetchPost, "updateServer.do", payload);
      if(response){
        yield put({
          type: 'updateServerItem',
          payload,
        });
      }
      return response;
    },
    *deleteServer({payload}, { put, call }){
      const response = yield call(fetchPost, "deleteServer.do", payload);
      if(response){
        yield put({
          type: 'deleteServerItem',
          payload,
        });
      }
      return response;
    },
    *execCommand({payload}, { put, call }){
      return yield call(fetchPost, "execCommand.do", payload);
    },
    *queryServerStatus({payload}, { call, put }){
      const response = yield call(fetchPost, "execCommand.do", payload);
      if(response){
        yield put({
          type: 'updateStatus',
          payload: response,
        });
      }
    }
  },

  reducers: {
    saveServer(state, {payload}){
      return {
        ...state,
        serverList: payload
      }
    },
    addServerItem(state, {payload}){
      return {
        ...state,
        serverList: state.serverList.concat([payload]),
        serverStatus: state.serverStatus.concat([{id: payload.id, status: 'unknow'}]),
      }
    },
    updateServerItem(state, {payload}){
      return {
        ...state,
        serverList: state.serverList.map(item => {
          return item.id === payload.id ? payload : item;
        })
      }
    },
    deleteServerItem(state, {payload}){
      return {
        ...state,
        serverList: state.serverList.filter(item => item.id !== payload.id)
      }
    },
    saveStatus(state, {payload}){
      return {
        ...state,
        serverStatus: payload
      }
    },
    updateStatus(state, {payload}){
      return {
        ...state,
        serverStatus: state.serverStatus.map(item => {
          if(item.id === payload.id){
            return payload;
          }else{
            return item;
          }
        })
      }
    }
  }

}

function getServer(server, sys){
  return {
    host: server.ip || sys.ip,
    port: server.port || sys.port,
    username: server.username || sys.username,
    password: server.password || sys.password,
  }
}
